package com.malou.client.ventas.tallas;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.MalouMessages;
import com.malou.client.util.Utilities;

public class TallaPanel extends ContentPanel {

	  TallaDialog dialog;
	  private BeanModel selectedModel;
	  private Button editTalla;
	  private Button deleteTalla;
	  private MalouConstants constants = GWT.create(MalouConstants.class);
	  private MalouMessages messages = GWT.create(MalouMessages.class);
      private List<BeanModel> selectedModels;
	  private Integer selectedSize;
	  private Button addTalla;
	  private ToolBar toolBar;
	  private SeparatorToolItem separatorEdit;
	  private SeparatorToolItem separatorAdd;
	  
	  public TallaPanel(){
		  
		  Tallas.getPaginate();
			
			final PagingToolBar toolBarPag = new PagingToolBar(Utilities.itemsPerPage);
		    toolBarPag.bind(Tallas.loader);
		    
	        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	        
	        final CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
	        
	        columns.add(sm.getColumn());
	        columns.add(new ColumnConfig("descripcion", constants.tallasDescripcion(), 150));  
	        
	        ColumnModel cm = new ColumnModel(columns);
	        
	        Grid<BeanModel> grid = new Grid<BeanModel>(Tallas.store, cm);  
	        grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {
	        	public void handleEvent(GridEvent<BeanModel> be) {  
	          	  Tallas.loader.load(0, Utilities.itemsPerPage);  
	            }  
	          });
	        
	        grid.setLoadMask(true);  
	        grid.setBorders(true);  
	        
	        this.setFrame(true);  
	        this.setCollapsible(true);  
	        this.setAnimCollapse(false);  
	        
	        this.setIcon(IconHelper.createPath("images/icons/table.png"));  
	        this.setHeading(constants.tallasLista());  
	        this.setLayout(new FitLayout());
	        
	        toolBar = new ToolBar(); 
	        addTalla = new Button(constants.agregar(), IconHelper.createPath("images/icons/new.gif"));
	        
	        dialog = new TallaDialog();			
			dialog.setStore(Tallas.store);
			
	        SelectionListener<ButtonEvent> sl_add;//obtienes un componente del boton.        
	        sl_add = new SelectionListener<ButtonEvent>(){
	        	
	        	@Override
				public void componentSelected(ButtonEvent ce) {//cuando haces click		
					dialog.reset();	
					dialog.unbind();
				    dialog.show();
				}
	        };
	        
	        addTalla.addSelectionListener(sl_add);
	        toolBar.add(addTalla);
	        separatorAdd = new SeparatorToolItem();
	        toolBar.add(separatorAdd);
	        deleteTalla = new Button(constants.eliminar(), IconHelper.createPath("images/icons/delete.gif"));
	        deleteTalla.setEnabled(false);
	        final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {  
	          public void handleEvent(MessageBoxEvent ce) {  
	            String button_id = ce.getButtonClicked().getItemId();  
	             if(Dialog.YES == button_id){
	            	 Tallas.tallaDialog = dialog;
	        		 
	            	 if(selectedSize == 1){
	            		 Integer id = selectedModel.get("id");            		 
	            		 Tallas.delete(id);
	            	 }else if(selectedSize > 1){
	            		 String ids = Utilities.toIds(selectedModels);            		 
	            		 Tallas.deleteAll(ids);
	            	 }
	             }
	          }  
	        };
	        
	        SelectionListener<ButtonEvent> sl_delete;
	        sl_delete = new SelectionListener<ButtonEvent>(){

				@Override
				public void componentSelected(ButtonEvent ce) {
					String mensaje="";
					if(selectedSize == 1){
						String descripcion = selectedModel.get("descripcion");
						
						mensaje = messages.tallasEstaSeguro(descripcion);					
					}else if(selectedSize > 1){
						mensaje = constants.eliminarAll();
					}
					MessageBox.confirm(constants.confirmacion(), mensaje, l).getDialog().getButtonById(Dialog.YES).setText(constants.si());
					
				}
				
			}; 
			
			deleteTalla.addSelectionListener(sl_delete);
	        toolBar.add(deleteTalla);
	        separatorEdit = new SeparatorToolItem();
	        toolBar.add(separatorEdit);  
	        
	        editTalla = new Button(constants.editar(), IconHelper.createPath("images/icons/update.png"));
	        editTalla.setEnabled(false);
	        toolBar.add(editTalla);
	        
	        SelectionListener<ButtonEvent> sl_edit;        
	        sl_edit = new SelectionListener<ButtonEvent>(){

				@Override
				public void componentSelected(ButtonEvent ce) {	
					dialog.bind(selectedModel);
					dialog.beforeEdit();
				    dialog.show();
				}
				
			};
			
			editTalla.addSelectionListener(sl_edit);
	        this.setTopComponent(toolBar); 
	                
	        grid.setSelectionModel(sm);
	        grid.getSelectionModel().addListener(Events.SelectionChange,
	                new Listener<SelectionChangedEvent<BeanModel>>() {

					@Override
					public void handleEvent(SelectionChangedEvent<BeanModel> be) {		
						selectedSize = be.getSelection().size();
						if(selectedSize==0){
							editTalla.setEnabled(false);
			            	dialog.unbind();
			            	deleteTalla.setEnabled(false);
						}else if(selectedSize==1){
							selectedModel = be.getSelectedItem();
							dialog.bind(selectedModel);
			            	editTalla.setEnabled(true);
			            	deleteTalla.setEnabled(true);
						}else if(selectedSize > 1){
							selectedModels = be.getSelection();						
							editTalla.setEnabled(false);						
							dialog.unbind();
							deleteTalla.setEnabled(true);
						}					
					}
	        	
	        });
	        
	        GridFilters filters = new GridFilters();
	        filters.getMessages().setFilterText(constants.filtros());
	        
	        StringFilter descripcionFilter = new StringFilter("descripcion");
	        
	        filters.addFilter(descripcionFilter);
	       
	        grid.addPlugin(filters);                
	        
	        this.setBottomComponent(toolBarPag);
	                
	        grid.addPlugin(sm);
	        
	        this.add(grid);
			this.setHeaderVisible(true);
	  }
	  
	public void setPermisos(List<String> permisos){
			
			boolean add = permisos.contains("add");
			boolean edit = permisos.contains("edit");
			boolean delete = permisos.contains("delete");
			
			if(!add && !edit && !delete){
				toolBar.setVisible(false);
			}else{
				if(!add){
					addTalla.setVisible(false);
					separatorAdd.setVisible(false);
				}
				if(!edit){
					editTalla.setVisible(false);
					separatorEdit.setVisible(false);
				}
				if(!delete)
					deleteTalla.setVisible(false);
			}
	}
}
